#
############################################################################
# (C) Copyright 2008 Novell, Inc. All Rights Reserved.
#
#  GPLv2: This program is free software; you can redistribute it
#  and/or modify it under the terms of version 2 of the GNU General
#  Public License as published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
############################################################################

makedir := $(dir $(lastword $(MAKEFILE_LIST)))
include $(makedir)/gnu.mk

LIBS   += -lrt -lm -lpthread -lncurses
INC    += -I.

ifeq ($(PN),)
  name := $(basename $(notdir $(PWD)))
else
  name := $(PN)
endif
target  := $(shell uname -m)
objdir  :=.$(target)
sources := $(wildcard *.c)
headers := $(wildcard *.h)
objects := $(addprefix $(objdir)/, $(sources:.c=.o))
opus    := $(objdir)/$(name)
bin      = $(DESTDIR)$(sbindir)

# -E stop after preprocessor
# -pg -O -g -DUNOPT -DNDEBUG
# CFLAGS+=-g -O -Wall -Wstrict-prototypes -Werror
CFLAGS +=-g -O -rdynamic \
	-Wall -Wstrict-prototypes -Werror \
	-D_F=\"$(basename $(notdir $(<)))\" \
	-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 \
	$(.INCLUDES) $(INC) \

objects += $(objdir)/syscall.o

$(opus):$(objects) $(LIBS)
	$(CC) $(CFLAGS) $(objects) $(LIBS) -o $(opus)

$(objdir)/%.o : %.c Makefile $(headers)
	@ mkdir -p $(objdir)
	$(CC) $(CFLAGS) -c $< -o $@

$(objdir)/syscall.o : $(objdir)/syscall.c
	$(CC) $(CFLAGS) -c $< -o $@

$(objdir)/syscall.c : Makefile syscall.awk
	@ mkdir -p $(objdir)
	echo '#include <sys/syscall.h>' | $(CC) -E -dD - -P | ./syscall.awk >$@

.PHONEY: install clean test

install:
	mkdir -p $(bin)
	cp $(opus) $(bin)

clean:
	@rm -fr $(objdir)
	@rm -f *.core
	@rm -f *.out
	@rm -f $(bin)/$(opus)

test:
	@ echo "Test"
	@ echo "sources=$(sources)"
	@ echo "objects=$(objects)"
	@ echo "CFLAGS=$(CFLAGS)"
	@ echo "BOARD=$(BOARD)"
	@ echo "bin=$(bin)"
	@ echo "objdir=$(objdir)"
	@ echo "opus=$(opus)"
	@ echo "name=$(name)"
